package com.deyun.smartairporthandset.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.deyun.smartairporthandset.R;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoFragment;
import com.jph.takephoto.model.TResult;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class PhotoFragment extends TakePhotoFragment {


    private Unbinder unbinder;
    private TakePhoto takePhoto;
    private File f;

    @BindView(R.id.iv_photo)
    ImageView iv_photo;
    @BindView(R.id.et_remark)
    EditText et_remark;

    @OnClick({R.id.tv_clean, R.id.tv_take_photo, R.id.tv_update})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.tv_clean:
                LogUtils.d("clean");
                f = null;
                iv_photo.setImageResource(0);
                et_remark.setText("");
                break;
            case R.id.tv_take_photo:
                takePhoto.onPickFromCapture(getImageCropUri());
                break;
            case R.id.tv_update:
                if (f != null) {
                    LogUtils.d("update");
                    f.delete();
                    f = null;
                    iv_photo.setImageResource(0);
                } else {
                    Toast.makeText(getActivity(), "您还没有拍照,不能上传", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        f = new File(result.getImage().getOriginalPath());
        Glide.with(getActivity()).load(result.getImage().getOriginalPath()).into(iv_photo);//
    }


    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    //获得照片的输出保存Uri
    private Uri getImageCropUri() {
        File file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
        return Uri.fromFile(file);
    }


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public PhotoFragment() {

    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MyFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PhotoFragment newInstance(String param1, String param2) {
        PhotoFragment fragment = new PhotoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_photo, container, false);
        unbinder = ButterKnife.bind(this, v);
        takePhoto = getTakePhoto();
        return v;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

}
